package com.dataseek.iot.operation.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.constant.ResponseConst;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.mysql.entity.Device;
import com.dataseek.iot.mysql.service.impl.AlarmLogServiceImpl;
import com.dataseek.iot.mysql.service.impl.DeviceServiceImpl;
import com.dataseek.iot.operation.manager.vo.LogParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author cc
 * @Title: 日志监控服务
 * @Description: 日志监控服务
 * @date 2021/10/295:09 下午
 * @since 1.0
 */
@RestController
@RequestMapping("/monitor")
@Api(tags = "日志查询")
@Slf4j
public class LogMonitorController {

    @Autowired
    AlarmLogServiceImpl alarmLogService;

    @Autowired
    DeviceServiceImpl deviceService;

    @PostMapping("/log/alarm")
    @ApiOperation("查询报警日志")
    public Flux<ResponseEntity> queryAlarmLog(@RequestBody LogParam logParam){
        ResponseEntity entity = new ResponseEntity();
        List<Device> devices = new ArrayList();
        try{
            if(Objects.isNull(logParam)){
                log.error("请求参数为空");
                return null;
            }
            QueryWrapper wrapper =  new QueryWrapper();
            //拼装条件查询
            if(Objects.nonNull(logParam.getDeviceCode())){
                wrapper.eq("device_code", logParam.getDeviceCode());
            }else if(Objects.nonNull(logParam.getLineCode())){
                QueryWrapper queryWrapper = new QueryWrapper();
                String codeStr = logParam.getLineCode();
                String[] codes = codeStr.split(",");
                for(String code : codes){
                    queryWrapper.like("line_code",code);
                    List<Device> result = deviceService.list(queryWrapper);
                    devices.addAll(result);
                }
                if(devices.size()>0){
                    List<String> deviceCodes = new ArrayList<>();
                    for(Device d : devices){
                        deviceCodes.add(d.getDeviceCode());
                    }
                    wrapper.in("device_code", deviceCodes);
                }else{
                    entity.setCode(ResponseConst.CRUD_FAILED_CODE);
                    entity.setMsg("未查询到数据");
                    entity.setData("");
                    return Flux.just(entity);
                }
            }
            if(Objects.nonNull(logParam.getAlarmType())){
                wrapper.eq("alarm_type", logParam.getAlarmType());
            }
            if(Objects.nonNull(logParam.getDate())){
                String[] times = logParam.getDate().split(",");
                wrapper.between("create_time", times[0], times[1]);
            }
            if(Objects.nonNull(logParam.getHandle_status())){
                wrapper.eq("handle_status", logParam.getHandle_status());
            }
            wrapper.orderByDesc("create_time");

            //拼装分页对象
            Page p = new Page();
            if(Objects.nonNull(logParam.getCurrent())){
                p.setCurrent(logParam.getCurrent());
            }
            if(Objects.nonNull(logParam.getSize())){
                p.setSize(logParam.getSize());
            }
            Page page = alarmLogService.page(p, wrapper);
            entity.setCode(ResponseConst.SUCCESS_CODE);
            entity.setMsg(ResponseConst.SUCCESS);
            entity.setData(page);
        }catch (Exception e){
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return Flux.just(entity);

    }


}
